package com.wx.file;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Date;

/**
 * File类的测试
 * @author wangxun
 * @version 1.0
 * @description: 对File类进行测试
 * @date 2021/1/21 3:15 下午
 */
public class FileTest {
    /**
     * new File 的几种方法
     * 文件可以是具体文件,也有可能是文件目录
     */
    @Test
    void newFileTest(){
        // 只是在内存中分配一个对象 , 在磁盘上并没有创建出来
        // 就算是创建出来,在虚拟文件系统中有,可能还没有到磁盘中,得看操作系统的本身的策略
        File directory = new File("/Users/wangxun/Downloads"+File.separator+"fileio");
        File a = new File("/Users/wangxun/Downloads/fileio", "a.txt");

        File b = new File(directory, "b.txt");

        File aDirectory = new File(directory, "new");

        showInfo(directory);
        showInfo(a);
        showInfo(b);
        showInfo(aDirectory);
    }

    /**
     * 创建文件
     */
    @Test
    void createFileTest() throws IOException {
        File directory = new File("/Users/wangxun/Downloads"+File.separator+"fileio");
        File b = new File(directory, "b.txt");
        if(!b.exists()){
            b.createNewFile();

        }
        showInfo(directory);

    }

    /**
     * 作业
     * 指定目录 计算目录占用的空间大小
     */
    @Test
    void sizeTest(){
        File file = new File("D:\\");
        if(file.isFile()){
            System.err.println("传入的不是目录");
            System.out.println("文件大小:"+file.length()+"B" );
            return;
        }
        long sumLength = 0L;
        TreeInfo rootInfo = computer(file, sumLength, 0);
        DirShowInfo(file.getAbsolutePath(),0,rootInfo);

    }

    TreeInfo computer(File file,long sumLength,int dept){
        File[] files = file.listFiles();
        dept ++;
        StringBuffer sb = new StringBuffer();

        for (File subFile : files) {

            if(subFile.isDirectory()){
                TreeInfo treeInfo = computer(subFile,0L,dept);
                DirShowInfo(subFile.getAbsolutePath(),dept,treeInfo);
                sumLength += treeInfo.getSize();

            }else {
                sumLength += subFile.length();
                sb.append(fileShowInfo(subFile,dept));

            }
        }



        return new TreeInfo(sumLength,sb.toString());
    }

    /**
     * 树的机构信息
      */
class TreeInfo{


        public Long getSize() {
            return size;
        }

        public void setSize(Long size) {
            this.size = size;
        }

        public String getSubInfo() {
            return subInfo;
        }

        public void setSubInfo(String subInfo) {
            this.subInfo = subInfo;
        }

        private Long size;
        private String subInfo;

    public TreeInfo(Long size, String subInfo) {
        this.size = size;
        this.subInfo = subInfo;
    }

        public TreeInfo(){

        }
}
    void DirShowInfo(String dirName,int dept,TreeInfo subInfo){
        StringBuffer sb = new StringBuffer();
        String flag = "";
        for (int i = 0; i < dept; i++) {
            flag +="\t";
        }
        sb.append(flag+"dir "+dirName+":"+subInfo.getSize()+"\n");
        sb.append(subInfo.getSubInfo());
        System.out.println(sb.toString());
    }

    String  fileShowInfo(File file,int dept){
        String flag = "";
        for (int i = 0; i < dept; i++) {
            flag +="\t";
        }
        return flag+"file "+file.getAbsolutePath()+":"+file.length()+"\n";
    }

    void  showInfo(File file){
        // 绝对路径
        System.out.println("绝对路径:"+file.getAbsolutePath());
        // 路径
        System.out.println("路径:"+file.getPath());

        // 父文件
        System.out.println("父文件:"+file.getParent());
        // 名字
        System.out.print("名字"+file.getName()+"  ==> ");
        System.out.println(file.isFile()?"文件":"目录");

        // 长度
        System.out.println("文件长度:"+file.length());
        // 最后修改时间
        System.out.println("最后修改时间:"+new Date(file.lastModified()));

        // 读写执行
        System.out.print("可读:"+file.canRead());
        System.out.print("   可写:"+file.canWrite());
        System.out.println("   可执行:"+file.canExecute());

        System.out.println("<=================我是分割线=====================>");
    }

    @Test
    void ByteArrayTest(){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(51);
        baos.write(52);
        baos.write(53);
        baos.write(54);
        baos.write(55);
        baos.write(56);
        System.out.println(baos.toString());
    }

    @Test
    void FileOutputTest()  {

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File("/Users/wangxun/Downloads/fileio", "b.txt"),true);
            fileOutputStream.write(51);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
